define(function(require, exports, module) {
    "use strict"; 
    var $ = require("$");
    require("echarts");
    var transMsgTime = function(a) {
        var b=new Date(a),c=new Date(),
        f={
            year: b.getFullYear(),
            month: b.getMonth() + 1,
            date: b.getDate(),
            day: b.getDay(),
            hours: b.getHours() >= 10 ? b.getHours() : "0" + b.getHours().toString(),
            min: b.getMinutes() >= 10 ? b.getMinutes() : "0" + b.getMinutes().toString()
        },r="";
        if(f.hours>0){
            r= f.hours+":"+f.min;
        }else{
            r= f.year+"-"+f.month+"-"+f.date;
        }
        return r;
    };

    return{
    	line:function(id,data,setConfig){
    		var myChart = echarts.init(document.getElementById(id));
    		var legends=[],unit=data[0].unit || "",x=[],xdata=data[0].xdate;
    		$(data).each(function(){
    			legends.push(this.name);
    		});
            for(var i=0,len=xdata.length;i<len;i++){
                if(!isNaN(xdata[i])&&(xdata[i]+"").length==10){
                    if(xdata[1]){

                    }
                    x.push(transMsgTime(1e3*xdata[i]));
                }else{
                    x.push(xdata[i]);
                }
            }
            
            var option = {
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                	y:"bottom",
                    data: legends
                },
                toolbox: {
                    show: true,
                    feature: { 
                        saveAsImage: { show: true }
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: x
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel: {
                            formatter: "{value} "+unit+""
                        }
                    }
                ],
                grid: {
                    borderWidth: 0,
                    borderColor:"red"
                },
                series: data
            };
            option=$.extend(true, option, setConfig);
            myChart.setOption(option);
    	},
        pie:function(id,data,setConfig){
            var myChart = echarts.init(document.getElementById(id));
            var legends=[],value=[],sum=0;
            $(data).each(function(){
                $(this.data).each(function(){
                    sum+=this.value;
                });
                
            });
            $(data).each(function(){
                $(this.data).each(function(){
                    legends.push(this.name);
                    var per=isNaN(((this.value/sum)*100).toFixed(2))?0:((this.value/sum)*100).toFixed(2);
                    value[this.name]=this.value+"("+per+"%)";
                });
                
            });
            var option = {
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient : 'vertical',
                    x : 'left',
                    y:"50px",
                    textStyle:{
                        align:"left"
                    },
                    selectedMode:false,
                    data:legends,
                    formatter:function(param){
                        return param+"   "+value[param]
                    }
                },
                toolbox: {
                    show : true,
                    feature : {
                        saveAsImage : {show: true}
                    }
                },
                calculable : false,
                series : data
            };

            option=$.extend(true, option, setConfig);
            myChart.setOption(option);
        },
        bar:function(id,data,setConfig){
            var myChart = echarts.init(document.getElementById(id));
            var legends=[],x=[],xdata=data[0].xdate;
            $(data).each(function(){
                legends.push(this.name);
            });
            for(var i=0,len=xdata.length;i<len;i++){
                if(!isNaN(xdata[i])&&(xdata[i]+"").length==10){
                    x.push(transMsgTime(1e3*xdata[i]));
                }else{
                    x.push(xdata[i]);
                }
            }
            var option = {
                title : {
                    text: '',
                    subtext: ''
                },
                tooltip : {
                    trigger: 'axis'
                },
                legend: {
                    y:"bottom",
                    data:legends
                },
                toolbox: {
                    show : true,
                    feature : {
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                xAxis : [
                    {
                        type : 'category',
                        data : x
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : data
            };
    

            option=$.extend(true, option, setConfig);
            myChart.setOption(option);
        }
    }
});